using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;

namespace DatabaseManager.AccessDatabase
{
	class AccessDatabseObject : DatabaseObjectBase
	{
		#region Fields
		int objectIndex;
		OleDbConnection databaseConnection;
		#endregion

		#region Properties
		public override int ObjectIndex
		{
			get
			{
				return this.objectIndex;
			}
			set
			{
				this.objectIndex = value;
			}
		}
		#endregion

		#region Constructors
		public AccessDatabseObject()
		{

		}
		#endregion

		#region Public methods

		public override void InitiateDatabaseConnection(string path, int index)
		{
			string connectionString = string.Format("provider=Microsoft.Jet.OLEDB.4.0; data source={0}", path);
			this.databaseConnection = new OleDbConnection(connectionString);
			this.objectIndex = index;
		}

		public override RecordSetObjectBase SelectRecords(string selectStatement)
		{
			OleDbCommand selectCommand = this.databaseConnection.CreateCommand();
			selectCommand.CommandText = selectStatement;
			this.databaseConnection.Open();

			AccessDatabaseRecordSet retrivedRecords = new AccessDatabaseRecordSet();
			retrivedRecords.DataReader = selectCommand.ExecuteReader();

			return retrivedRecords;
		}

		public override void CloseDatabaseConncetion()
		{
			this.databaseConnection.Close();
		}		
		#endregion
	}
}
